var express = require("express");
var router = express.Router()
var {checkSession} = require("../utils")
var {Mvs,Comment} = require("../utils/model")


router.get("/index",(req,res)=>{
    checkSession(req,res,()=>{
        const findOneData = function(obj,callback){
            Mvs.findOne(obj)
            .then(result=>{
                callback(result)
            })
        }
        var {mid} = req.query;
        if(mid){
            findOneData({id:mid},(result)=>{
                Comment.find({mid})
                .limit(3)
                .sort({time:-1})
                .then(data=>{
                    res.render("comment",{username:req.session.username,result,data,mid})
                })
              
            })
        }else{
            findOneData({},(result)=>{
                Comment.find({mid})
                .limit(5)
                .sort({time:-1})
                .then(data=>{
                    res.render("comment",{username:req.session.username,result,data,mid})
                })
             
            })
        }

   
    })
})

router.post("/submit",(req,res)=>{
    const query = req.query;
    const body = req.body;
    checkSession(req,res,()=>{
        Mvs.findOne({
            id:query.mid
        }).then(result=>{
            console.log(result)
            var data = new Date();
            var y = data.getFullYear();
            var m = data.getMonth()+1;
            var d = data.getDate(); 
            var h = data.getHours();
            var minutes = data.getMinutes();
            body.time = `${y}/${m}/${d} ${h}:${minutes}`
            body.author = req.session.username;
            body.mid = query.mid;
            body.movie = result;
            Comment.insertMany(body)
            .then(result=>{
                res.redirect("/comment/mlist")
            })
        })
    })
})

router.get("/message",(req,res)=>{
    checkSession(req,res,()=>{
        const {mid} = req.query
        Comment.find({mid})
        .sort({time:-1})
        .then(data=>{
            res.render("message",{username:req.session.username,data})
        }) 
    })
})

router.post("/message",(req,res)=>{
    checkSession(req,res,()=>{
        var name = req.session.username
        const body = req.body
        var obj = {name,say:body.say}
        console.log(obj)
        Comment.find({_id:body.mid})
        .then(result=>{          
                var arr = result.answer
                arr.push(obj)
                console.log(arr)
            Comment.update({_id:body.mid},{$set:{answer:arr}})
            .then(result=>{
                if(result){
                    Comment.find({_id:body.mid})
                    .then(result=>{
                        console.log(result)
                        res.json({
                            result
                        })
                    })
                }
            })
        })
    })
})

// 评论列表
router.get("/mlist",(req,res)=>{
    checkSession(req,res,()=>{
        // 分页 
        var {
            pageSize,
            page
        } = req.query;
        var total = 0;
        var totalPage = 0;
        pageSize = pageSize *1 || 10;   // 每页的数量
        page = page *1  || 1;   // 页码

        Comment.find({},{})
        .then(data=>{
            total  = data.length;
            totalPage = Math.ceil(total/pageSize);
            page  = page < 1 ? 1 : page;
            page = page > totalPage ? totalPage : page;
            Comment.find({},{})
            .skip((page-1)*pageSize)
            .limit(pageSize)
            .sort({_id:-1})
            .then(result=>{
                result = result.map(item=>{
                    item = {...item._doc,time:item.time}
                    return item;
                })
                res.render('mlist',{
                    username:req.session.username,
                    result,
                    total,
                    page,
                    pageSize,
                    totalPage
                })
            })
        })
    })
})

router.get("/moviepin",(req,res)=>{
    checkSession(req,res,()=>{
        var {mid} = req.query;
        Comment.find({
            mid
        },{})
        .sort({_id:-1})
        .then(result=>{
            result = result.map(item=>{
                item = {...item._doc,time:item.time}
                return item;
            })
            res.render("moviepin",{
                result,
                movie:result[0].movie,
                username:req.session.username
            })
        })
    })
})

module.exports = router;


